﻿$(function () {
    $.kish.carAnnListEdit = {};
    $.extend($.kish.carAnnListEdit, {
    WithTotal : null,
        initializing: function () {
            $.kish.carAnnListEdit.ListCarAnn(0,30,true);
        },

        //*************************//
        ListCarAnn: function (start, end, withtotal) {
            $.ajax({
                url: '../../KishCar/Services/CarAnn.svc/GetAllUserCarAnn',
                data: JSON.stringify({ Start: start, End: end, WithTotal: withtotal }),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    if (data['d'] != null) {
                        if (data['d'].length == 0) {
                            $.kish.message.getErrorMessage("15");
                        }
                        else {
                            var tableHeader = '<tr><th >تصویر</th><th >مدل خودرو</th><th >تاریج انقضا</th><th >وضعیت</th><th>ویرایش</th><th>حذف</th></tr>';
                            $('.carAnnTable').append(tableHeader);
                            for (var i = 0; i < data['d'].Data.length; i++) {
                                var path = '';
                                if (data['d'].Data[i].Car.CarImages[0] != null) {
                                    path = (data['d'].Data[i].Car.CarImages[0].Path);
                                    var relatedPath = '../' + path.replace('~', '..');
                                    var Image = '<a  class="highslide" href="' + relatedPath + '"> <img  class="width80 fn_prodImg " alt="' + data['d'].Data[i].Car.Model.Code + '" title="تصویر بزرگتر" src= "' + relatedPath + '"/></a>';
                                }
                                var x = '<tr id=' + data['d'].Data[i].Car.ID + '><td >' + (path ? Image : "بدون تصویر") + ' </td><td class=" breakWord">' + data['d'].Data[i].Car.Model.Code + ' </td ><td>' + $.trim(data['d'].Data[i].strFinishDate) + '</td><td class="width80">' + (data['d'].Data[i].IsActive ? "تایید شده" : "منتظر تایید") + '</td><td><img alt="ویرایش" title="ویرایش" class="noBorder  inline-block pointer fn_edit" src="../../Images/ico/edit.gif" /></td><td><img alt="حذف" title="حذف" src="../../Images/ico/delete.gif" class="noBorder inline-block pointer fn_del" /></td></tr>';
                                $('.carAnnTable').append(x);
                            };

                            $('.fn_del').click(function () {
                                var thisRow = $(this).closest("tr");
                                $.kish.carAnnListEdit.DelCarAnn(thisRow);
                            });

                            $('.fn_edit').click(function () {
                                var carAnnID = $(this).closest("tr").attr('id');
                                window.location.replace("./CarAddEdit.aspx?ID=" + carAnnID);
                            });

                            $('a.highslide').click(function () {
                                return hs.expand(this)
                            });

                            if (withtotal && data['d'].TotalCount && data['d'].TotalCount > 0 && data['d'].TotalCount != null) {
                                $.kish.carAnnListEdit.WithTotal = data['d'].TotalCount;
                                $.kish.carAnnListEdit.SetPaging();
                            }
                        }
                    }
                }
            });
        },

        SetPaging: function () {
            var withTotal = $.kish.carAnnListEdit.WithTotal;
            var PageCount = Math.ceil(withTotal / 30);
            var paging = '';
            for (var i = 0; i < PageCount; i++) {
                var PageItem = '<span class="pagingItem">' + (i + 1).toString() + '</span>';
                paging = paging + PageItem;
            }
            $('.PagingDiv').text('');
            $('.PagingDiv').append(paging);
            $('.PagingDiv').append('<span class="totalCount"> کل موارد یافت شده : ' + withTotal + '</span>');
            $('.pagingItem', '.PagingDiv').click(function () {
                var pageIndex = $(this).text();
                $.kish.carAnnListEdit.ListCarAnn((pageIndex - 1) * 30, pageIndex * 30, false);
            });
        },
        //*************************//
        DelCarAnn: function (thisRow) {
            var carAnnID = thisRow.attr('id');
            $.kish.message.getConfirmationMessage(1, function () {

                $.ajax({
                    'type': 'POST',
                    'url': '../Services/CarAnn.svc/DelCarAnn',
                    'data': JSON.stringify({ "ID": carAnnID }),
                    'contentType': 'application/json; charset=utf-8',
                    'dataType': 'json',
                    'success': function (result) {
                        if (result['d'].IsSucceed) {
                            $(thisRow).remove();
                            //                            $.kish.carAnnListEdit.ListCarAnn();
                            $.kish.message.showUserMessage(result['d'].Message.Message);
                        }
                        else {
                            $.kish.message.showErrorMessage(result['d'].Message.Message);
                        }
                    },
                    'error': function () {
                        $.kish.message.alertError("خطا در سیستم");
                    }
                });

            }, function () {
                // $this.show();
            });
        }

    });
    $.kish.carAnnListEdit.initializing();
});